<?php

namespace App\Http\Controllers\API;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Redis;
use phpDocumentor\Reflection\Types\Self_;

class MenuController extends Controller
{
    public function get_access_token()
    {
        if (!Redis::exists('weixin_access_token')) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . config('weixin.appid') . "&secret=" . config('weixin.secret');
            $ch = curl_init();
            // 设置URL和相应的选项
            curl_setopt($ch, CURLOPT_URL, $url); // 需要获取的 URL 地址，也可以在 curl_init() 初始化会话的时候。
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
            curl_setopt($ch, CURLOPT_HEADER, false); // 启用时会将头文件的信息作为数据流输出。
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 在尝试连接时等待的秒数。设置为 0，则无限等待。
            curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 允许 cURL 函数执行的最长秒数。
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE 将 curl_exec() 获取的信息以字符串返回，而不是直接输出。
            // 抓取 URL 并把它传递给浏览器
            $token = curl_exec($ch);
            // 关闭 cURL 资源，并且释放系统资源
            curl_close($ch);
            Redis::setex('weixin_access_token',6900,$token);
            return json_decode($token, true);
        }else {
            return json_decode(Redis::get('weixin_access_token'), true);
        }

    }

    public function get_wechat($url)
    {
        $ch = curl_init();
        // 设置URL和相应的选项
        curl_setopt($ch, CURLOPT_URL, $url); // 需要获取的 URL 地址，也可以在 curl_init() 初始化会话的时候。
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_HEADER, false); // 启用时会将头文件的信息作为数据流输出。
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 在尝试连接时等待的秒数。设置为 0，则无限等待。
        curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 允许 cURL 函数执行的最长秒数。
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE 将 curl_exec() 获取的信息以字符串返回，而不是直接输出。
        // 抓取 URL 并把它传递给浏览器
        $token = curl_exec($ch);
        // 关闭 cURL 资源，并且释放系统资源
        curl_close($ch);
        return json_decode($token, true);
    }

    public function https_request($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        if (array_key_exists('access_token', $this->get_access_token())) {
            //$access_token = $this->get_access_token()['access_token'];
            $access_token = $this->get_access_token()['access_token'];
            $get_menu = 'https://api.weixin.qq.com/cgi-bin/menu/get?access_token='.$access_token;
            return $this->get_wechat($get_menu);
        } else {
            return $this->get_access_token();
        }
//        return \Redis::command('keys',['*']);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->getContent();
        if (array_key_exists('access_token', $this->get_access_token())) {
            $access_token = $this->get_access_token()['access_token'];
            $created_menu = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='.$access_token;
            return $this->https_request($created_menu, $data);
        } else {
            return $this->get_access_token();
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy()
    {
        if (array_key_exists('access_token', $this->get_access_token())) {
            //$access_token = $this->get_access_token()['access_token'];
            $access_token = $this->get_access_token()['access_token'];
            $delete_menu = 'https://api.weixin.qq.com/cgi-bin/menu/delete?access_token='.$access_token;
            return $this->get_wechat($delete_menu);

        } else {
            return $this->get_access_token();
        }

    }
}
